<?php session_start();
include("userfunctions.php");
include("../dbconnect.php");
$i = 1;
if ($_POST['email'] == $_POST['email1']){
	while (isset ($_POST['email'.$i])){
		if (check_email_address($_POST['email'.$i]) == true){
			$prim = 0;
			if ($_POST['primary'] == $i){
				$prim = 1;
			}
			$query = sprintf("Insert into Email (PeId, EmailAdress, Prim) values (%d, '%s', %b)",
			$_SESSION['PersonId'], $db->real_escape_string($_POST['email'.$i]), $prim);
			$db->query($query);
		}
		$i++;
	}

	if (isset ($_POST['twitter'])){
		$query = sprintf("Insert into Twitter (PeId, TwitterName) values (%d, '%s')",
		$_SESSION['PersonId'], $db->real_escape_string($_POST['twitter']));
		$db->query($query);
	}
	
	$my_t=getdate(date("U"));
	$activationcode = $my_t[year].$my_t[mon].$my_t[wday].$my_t[mday].$my_t[hours].$my_t[minutes].$my_t[seconds];
	$query = sprintf("Insert into Activation (PeId, ActivationCode) values (%d, %d)",
	$_SESSION['PersonId'], $activationcode);
	$db->query($query);
	
	sendMail("Your registration on MsgHub was successful. \n To finish the registration please login and enter you activationcode. \n\n Activationcode:		$activationcode \n \n kind regards \n your MsgHub-team", "Your activationcode", $_POST['email']);
	
	echo "<script type=\"text/javascript\"> alert(\"The signup process is complete, now you can login\")</script>";
	header ("Location:../../login.php");
}else{
	echo "<script type=\"text/javascript\"> alert(\"The email adresses doesn't match\")</script>";
	header ("Location:../../signuppart2.php");
}

?>